package com.lzk.user.serice.impl;

import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.basic.util.Md5Utils;
import com.lzk.basic.util.StrUtils;
import com.lzk.basic.constants.RegisterConstant;
import com.lzk.user.domain.LoginInfo;
import com.lzk.user.domain.User;
import com.lzk.user.dto.UserDto;
import com.lzk.user.mapper.LoginInfoMapper;
import com.lzk.user.mapper.UserMapper;
import com.lzk.user.serice.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    /**
     * 用户通过手机号注册
     * @param userDto
     */
    @Override
    public void phoneRegister(UserDto userDto) {
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String password = userDto.getPassword();
        String confirmPassword = userDto.getConfirmPassword();
        //1.校验
        //1.1.空值校验
        if(StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(phoneCode)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(confirmPassword)){
            throw new BusinessException("参数不能为空");
        }
        //1.2.两次密码是否相同
        if(!password.equals(confirmPassword)){
            throw new BusinessException("两次密码不一致!!!");
        }
        //1.3.验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.REGISTER_CODE_PREFIX);
        if(obj == null){
            throw new BusinessException("验证码失效，请重新获取!");
        }
        //1.4.验证码是否正确  //
        String codeTmp = obj.toString().split(":")[0];
        if(!codeTmp.equalsIgnoreCase(phoneCode)){//equalsIgnoreCase不区别大小写
            throw new BusinessException("验证码错误!");
        }
        //2.保存数据
        //2.1.先保存t_logininfo
        User user = userDto2User(userDto);
        LoginInfo logininfo = user2Logininfo(user);
        loginInfoMapper.save(logininfo);//自增
        //2.2.再保存t_user
        user.setLogininfo_id(logininfo.getId());
        //user.setLogininfo(logininfo);
        userMapper.save(user);
    }

    public User userDto2User(UserDto userDto){
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码 加密加盐
        String md5Password = Md5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Password);
        return user;
    }

    public LoginInfo user2Logininfo(User user){
        LoginInfo loginInfo = new LoginInfo();
        //按照同名属性赋值 beanUtils.copyProperties
        BeanUtils.copyProperties(user,loginInfo);
        //当前对象是普通用户
        loginInfo.setType(1);
        return loginInfo;
    }
}
